#include <retro.h>
#include "retro.creator.h"

using namespace Retro::Creator;
using namespace Retro::Creator::Dialog;
using namespace SPLib::Windows;

enum OptionsWindow
{
  OptionsTabs = 0x0010,
};

static PCTSTR gPageNames[] =
{
  _T("General"),
  _T("Editor"),
  _T("Advanced"),
};

Options::Options() :
  mHeadingFont(_T("Microsoft Sans Serif"), 10, Gdip::FontStyleBold)
{
  mContent = NULL;
}

Options::~Options()
{
  for (UINT i = 0; i < ARRAYSIZE(mPages); ++i)
    mPages->clear();
}

VOID    Options::setState(const FrameState& iState)
{
  mGridColor.setColor(iState.getData().gridColor);

  mTabs.selectTab(0);
}

VOID    Options::getState(FrameState& oState)
{
  oState.getData().gridColor = mGridColor.getColor();
}

PCTSTR  Options::tabTextGetter(TabItem* iItem)
{
  return gPageNames[(UINT)iItem->data];
}

VOID    Options::onNotify(Event::NotifyEvent& iEvent)
{
  switch (iEvent.data->idFrom)
  {
  case OptionsTabs:
    switch (iEvent.data->code)
    {
    case TabNotifyTabSelect:
      if (mContent)
      {
        mContent->clear();
        mContent->margins.setAll(10);
        mContent->addElement(&mPages[((TabNotify*)iEvent.data)->tabIndex]);
        doLayout();
      }
      break;
    }
    break;
  case IDOK:
    setResult(DialogOK);
  case IDCANCEL:
    hide();
    break;
  }
}

VOID    Options::onLoad()
{
  LayoutElement* wLayout;

  mTabs.create(*this);
  mTabs.setTextGetter(tabTextGetter);
  mTabs.addStyle(WS_TABSTOP);
  mTabs.setID(OptionsTabs);

  getLayout().padding.setHoriz(0);
  getLayout().addTop(23)->addFill(mTabs);

  mContent = getLayout().addTop(200);

  for (UINT i = 0; i < ARRAYSIZE(gPageNames); ++i)
  {
    mPages[i].dock = DockFill;
    mTabs.addTab((PVOID)i, 0);
  }

  {
    mGridColor.create(*this);
    mGridColor.setColor(0xFFFFFFFF);

    mGridColorLabel.create(*this);
    mGridColorLabel.setFont(&mHeadingFont);
    mGridColorLabel.setForeColor(0xFF444444);
    mGridColorLabel.setText(_T("Grid Color"));

    wLayout = mPages[OptionsEditor].addTop(150);
    wLayout->addTop(25, mGridColorLabel);
    wLayout->addFill(mGridColor);
  }

  addOKCancel(mOK, mCancel, mSeparator)->margins.setHoriz(10);
}

VOID    Options::onUnload()
{
}
